﻿@model Kooboo.CMS.Web.Areas.Contents.Models.MediaContentGrid
@using Kooboo.CMS.Content.Models
@{
    Layout = "~/Views/Shared/Popup.cshtml";

    ViewBag.Title = "Media Library".Localize();

    var routes = ViewContext.RequestContext.AllRouteValues();

    var fileType = ViewContext.RequestContext.GetRequestValue("fileType");

    var fileTypeDic = new Dictionary<string, List<string>>();
    fileTypeDic.Add("image", new List<string> { "jpg", "png", "gif", "jpeg", "bmp" });

    var displayExtension = string.IsNullOrWhiteSpace(fileType) ? new List<string>() : fileTypeDic.ContainsKey(fileType) ? fileTypeDic[fileType.ToLower()] : new List<string>();

    string action = routes["action"].ToString();
    var folderPaths = (ViewContext.RequestContext.GetRequestValue("FolderName") != null ? FolderHelper.SplitFullName(ViewContext.RequestContext.GetRequestValue("FolderName")) : new string[] { }).ToArray();

    var childFolders = Model.ChildFolders.ToArray();
    var images = Model.Contents == null ? new MediaContent[0] : Model.Contents.Where(it => it.IsImage).ToArray();
    var files = Model.Contents == null ? new MediaContent[0] : Model.Contents.Where(it => !it.IsImage).ToArray();

    var singleChoice = !string.IsNullOrEmpty(ViewContext.RequestContext.GetRequestValue("SingleChoice"));
}
<div id="main-container">
    <div class="wrap">
        <div class="topbar clearfix">
            <h1 class="title">
                @ViewBag.Title <strong>@ViewContext.RequestContext.GetRequestValue("folderPath")</strong>
            </h1>

            @if (folderPaths.Length > 0)
            {
                <div class="breadcrumb">
                    <a href="@Url.Action(action, ViewContext.RequestContext.AllRouteValues().Merge("FolderName", ""))">@("Root".Localize())</a>

                    @for (var i = 0; i < folderPaths.Length; i++)
                    {
                        if (i == folderPaths.Length - 1)
                        {
                            @Html.IconImage("arrow gray-right")
                            <span>@folderPaths[i]</span>
                        }
                        else
                        {
                            @Html.IconImage("arrow gray-right")
                            <a href="@Url.Action(action, ViewContext.RequestContext.AllRouteValues().Merge("FolderName", FolderHelper.CombineFullName(folderPaths.Take(i + 1))))">@folderPaths[i]</a>
                        }
                    }
                </div>
            }

            @(Html.Partial("_Search"))
            <ul class="view-switch">
                <li class="list"><a title="@("Details view".Localize())" href="@Url.Action(ViewContext.RequestContext.GetRequestValue("action"), ViewContext.RequestContext.GetRequestValue("controller"), ViewContext.RequestContext.AllRouteValues().Merge("listType", null))">@Html.IconImage("list")</a></li>
                <li class="grid active"><a title="@("Thumbnails view".Localize())" href="@Url.Action(ViewContext.RequestContext.GetRequestValue("action"), ViewContext.RequestContext.GetRequestValue("controller"), ViewContext.RequestContext.AllRouteValues().Merge("listType", "Grid"))">@Html.IconImage("grid")</a></li>
            </ul>
        </div>

        <div class="common-grid fixed">
            @if (childFolders.Length == 0 && files.Length == 0 && images.Length == 0)
            {
                <p>@("This folder is empty.".Localize())</p>
            }
            @if (childFolders.Length > 0)
            {
                <ul class="folder clearfix">
                    @foreach (var folder in childFolders)
                    {
                        <li>
                            <a href="@this.Url.Action(action, ViewContext.RequestContext.AllRouteValues().Merge("FolderName", folder.FullName))">
                                <span class="info">@folder.Name</span>
                            </a>
                        </li>
                    }
                </ul>
            }
            @if (files.Length > 0)
            {
                <ul class="file clearfix">
                    @foreach (var file in files)
                    {
                        <li>
                            <a class="select">
                                <input type="@(singleChoice ? "radio" : "checkbox")" name="select" value="@Html.Raw(file.UUID)" class="select doc"
                                       data-alt-text="@file.FileName"
                                       data-is-image="@file.IsImage"
                                       data-url="@Html.Raw(file.Url)" autocomplete="off" />
                                <span class="checked"></span>
                                <span class="info">@Html.IconImage("file") @file.FileName</span>
                            </a>
                        </li>
                    }
                </ul>
            }
            @if (images.Length > 0)
            {
                <ul class="file image clearfix">
                    @foreach (var image in images)
                    {
                        <li>
                            <a class="select">
                                <input type="@(singleChoice ? "radio" : "checkbox")" name="select" value="@Html.Raw(image.UUID)" class="select doc"
                                       data-alt-text="@Html.Raw((image.Metadata == null || string.IsNullOrEmpty(image.Metadata.AlternateText)) ? image.FileName : image.Metadata.AlternateText)"
                                       data-is-image="@image.IsImage"
                                       data-url="@Html.Raw(image.Url)" />
                                <span class="checked"></span>
                                <img src="@Url.Action("ResizeImage", "Resource", new { siteName = ViewContext.RequestContext.GetRequestValue("SiteName"), url = image.VirtualPath, area = "", width = 0, height = 120, preserverAspectRatio = true, quality = 80 })" />
                            </a>
                        </li>
                    }
                </ul>
            }
            <div class="pagination">
                @(Html.Pager(Model.Contents, "", ViewContext.RequestContext.AllRouteValues(), null))
            </div>
        </div>
    </div>
</div>
<div class="buttons">

    @if (folderPaths.Length > 0)
    {
        <a class="upload-button button upload left">
            <span>
                <form action="@this.Url.Action("Upload", ViewContext.RequestContext.AllRouteValues().Merge("return", ViewContext.HttpContext.Request.RawUrl))" enctype="multipart/form-data" method="post" class="no-stop">
                    <input type="hidden" value="True" name="Overrided" />
                    <input type="file" name="file" title="@("Upload".Localize())" />
                </form>
            </span>
            @("Upload".Localize())
        </a>
    }
    <button id="addBtn">
        @("Insert".Localize())
    </button>

    <a id="cancelBtn" class="button gray cancel action">@("Cancel".Localize())</a>
</div>
<script type="text/javascript">
    $(function () {
        $('.common-grid').checkableMetro();
        $('.common-grid').mixedGrid();

        $('a.select').click(function (e) {

            var $checkbox = $(this).find('input:checkbox.select');
            var $radio = $(this).find('input:radio.select');
            $checkbox.attr('checked', !$checkbox.attr('checked'));
            $(this).parent().toggleClass('active');
            if ($radio != null) {
                $radio.attr('checked', 'checked');
                $(this).parent().addClass('active');
                $(this).parent().siblings('.active').removeClass('active');
            }
        });
    });
</script>
@Html.Partial("Selection_Script")